﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Castle.ActiveRecord;
using Castle.ActiveRecord.Queries;
using NHibernate.Criterion;
using System.Collections;

/// <summary>
/// Summary description for Tour
/// </summary>
namespace LIB
{
    [ActiveRecord("Category")]
    public class Category_Info : ActiveRecordBase<Category_Info>
    {
        private int _id;
        private string _catName;
        private string _catDes;
        private string _catAvatar;
        private int _catOrder;
        private string _catMetaDes;
        private string _catMetaTitle;
        private string _catMetaKey;
        private int _catLang;
        private bool _isHomePage;
        private Category_Info _catParentID;
        private IList<Category_Info> _lstChildCat;
        private IList<Content_Info> _lstContent;

        [PrimaryKey(PrimaryKeyType.Native)]
        public int id
        {
            get { return _id; }
            set { _id = value; }
        }
        [Property]
        public string catName
        {
            get { return _catName; }
            set { _catName = value; }
        }
        [Property]
        public string catDes
        {
            get { return _catDes; }
            set { _catDes = value; }
        }
        [Property]
        public string catAvatar
        {
            get { return _catAvatar; }
            set { _catAvatar = value; }
        }
        [Property]
        public int catOrder
        {
            get { return _catOrder; }
            set { _catOrder = value; }
        }
        [Property]
        public int catLang
        {
            get { return _catLang; }
            set { _catLang = value; }
        }
        [Property]
        public string catMetaDes
        {
            get { return _catMetaDes; }
            set { _catMetaDes = value; }
        }
        [Property]
        public string catMetaTitle
        {
            get { return _catMetaTitle; }
            set { _catMetaTitle = value; }
        }
        [Property]
        public string catMetaKey
        {
            get { return _catMetaKey; }
            set { _catMetaKey = value; }
        }
        [Property]
        public bool isHomePage
        {
            get { return _isHomePage; }
            set { _isHomePage = value; }
        }
        [BelongsTo("catParentID")]
        public Category_Info catParentID
        {
            get { return _catParentID; }
            set { _catParentID = value; }
        }

        [HasMany(typeof(Category_Info), Table = "Category", ColumnKey = "catParentID", Lazy = true, Cascade = ManyRelationCascadeEnum.AllDeleteOrphan)]
        public IList<Category_Info> lstChildCat
        {
            get { return _lstChildCat; }
            set { _lstChildCat = value; }
        }
        public static Category_Info[] searchInQuanHuyen(int quanHuyenID)
        {
            SimpleQuery q = new SimpleQuery(typeof(Category_Info), QueryLanguage.Sql, @"EXEC searchInQuanHuyen @quanHuyenID=?", new object[] { quanHuyenID });
            q.AddSqlReturnDefinition(typeof(Category_Info), "listDes");
            Category_Info[] des = (Category_Info[])ExecuteQuery(q);
            return des;
        }
        public static Category_Info[] getCatInDuAn(int quanHuyenID,int loaiDAID)
        {
            SimpleQuery q = new SimpleQuery(typeof(Category_Info), QueryLanguage.Sql, @"EXEC getCatInDuAn @quanHuyenID=?,@loaiDAID=?", new object[] { quanHuyenID,loaiDAID });
            q.AddSqlReturnDefinition(typeof(Category_Info), "listDes");
            Category_Info[] des = (Category_Info[])ExecuteQuery(q);
            return des;
        }
        #region function
       
        #endregion
    }

     
    public class Category_Info_Sorter : IComparer
    {
        public int Compare(object x, object y)
        {
            return ((Category_Info)x).catOrder.CompareTo(((Category_Info)y).catOrder);
        }
    }

    [ActiveRecord("catDeQuy")]
    public partial class catDeQuy : ActiveRecordBase<catDeQuy>
    {
        private int _STT;
        private int _catParentID;
        private int _id;
        private string _catName;
        private string _catDes;
        private string _catAvatar;
        private int _catOrder;
        private bool _isHomePage;
        private int _Level;
        private string _Sort;

       
        [Property]
        public virtual int STT
        {
            get { return this._STT; }
            set { this._STT = value; }
        }
        [Property]
        public virtual int catParentID
        {
            get { return _catParentID; }
            set { _catParentID = value; }
        }
         [PrimaryKey(PrimaryKeyType.Assigned, "id")]
        public virtual int id
        {
            get { return _id; }
            set { _id = value; }
        }
        [Property]
        public virtual string catName
        {
            get { return _catName; }
            set { _catName = value; }
        }
        [Property]
        public virtual string catAvatar
        {
            get { return _catAvatar; }
            set { _catAvatar = value; }
        }
        [Property]
        public virtual string catDes
        {
            get { return _catDes; }
            set { _catDes = value; }
        }
        [Property]
        public virtual int catOrder
        {
            get { return _catOrder; }
            set { _catOrder = value; }
        }
        [Property]
        public virtual bool isHomePage
        {
            get { return _isHomePage; }
            set { _isHomePage = value; }
        }

        [Property]
        public virtual int Level
        {
            get { return _Level; }
            set { _Level = value; }
        }
        [Property]
        public virtual string Sort
        {
            get { return _Sort; }
            set { _Sort = value; }
        }

        public static catDeQuy[] Category_Dequy(int MaParent, int maxLevel,string space)
        {
            SimpleQuery q = new SimpleQuery(typeof(catDeQuy), QueryLanguage.Sql, @"EXEC Category_Dequy @MaParent=?,@maxLevel=?,@space=?", new object[] { MaParent,maxLevel,space });
            q.AddSqlReturnDefinition(typeof(catDeQuy), "listDes");
            catDeQuy[] des = (catDeQuy[])ExecuteQuery(q);
            return des;
        }
        public static catDeQuy[] getListChuDe(int MaParent, int maxLevel, string space)
        {
            SimpleQuery q = new SimpleQuery(typeof(catDeQuy), QueryLanguage.Sql, @"EXEC getListChuDe @MaParent=?,@maxLevel=?,@space=?", new object[] { MaParent, maxLevel, space });
            q.AddSqlReturnDefinition(typeof(catDeQuy), "listDes");
            catDeQuy[] des = (catDeQuy[])ExecuteQuery(q);
            return des;
        }
        public static catDeQuy[] getCatInHome(int lang)
        {
            SimpleQuery q = new SimpleQuery(typeof(catDeQuy), QueryLanguage.Sql, @"EXEC getCatInHome @lang=?", new object[] { lang });
            q.AddSqlReturnDefinition(typeof(catDeQuy), "listDes");
            catDeQuy[] des = (catDeQuy[])ExecuteQuery(q);
            return des;
        }
    }
    [ActiveRecord("catDeQuyNguoc")]
    public partial class catDeQuyNguoc : ActiveRecordBase<catDeQuyNguoc>
    {
        private int _id;
        private string _catName;
        private int _Level;
                
        [PrimaryKey(PrimaryKeyType.Assigned)]
        public virtual int id
        {
            get { return _id; }
            set { _id = value; }
        }
        [Property]
        public virtual string catName
        {
            get { return _catName; }
            set { _catName = value; }
        }
        
        [Property]
        public virtual int Level
        {
            get { return _Level; }
            set { _Level = value; }
        }


        public static catDeQuyNguoc[] getLinkCat(int catID)
        {
            SimpleQuery q = new SimpleQuery(typeof(catDeQuyNguoc), QueryLanguage.Sql, @"EXEC getLinkCat @catID=?", new object[] {catID });
            q.AddSqlReturnDefinition(typeof(catDeQuyNguoc), "listDes");
            catDeQuyNguoc[] des = (catDeQuyNguoc[])ExecuteQuery(q);
            return des;
        }
    }
}
